const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-') 
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const is_video = (value)=>{
  var reg = new RegExp(/\.(mp4|mov|avi|wmv|mpeg)$/,'gi')
    return reg.test(value)
}
const img_list = {
  "address-one": "//img.xingkonglian.net/img/weapp/address-none.png",
  "audit": "https://img.xingkonglian.net/img/common/audit1.png",
  //"b_icon1": "//img.xingkonglian.net/img/weapp/b_icon1.png",
  //"b_icon2": "//img.xingkonglian.net/img/weapp/b_icon2.png",
  //"b_icon3": "//img.xingkonglian.net/img/weapp/b_icon3.png",
  "copy_box_bg": "//img.xingkonglian.net/img/weapp/copy_box_bg.png",
  "example": "//img.xingkonglian.net/img/weapp/example.png",
  "fanmian": "//img.xingkonglian.net/img/weapp/fanmian.png",
  "grow_bg": "https://img.xingkonglian.net/img/common/grow_bg2.png",
  "loading": "//img.xingkonglian.net/img/weapp/loading.gif",
  "logo": "//img.xingkonglian.net/img/weapp/logo.png",
  "my_qrcode_img": "https://img.xingkonglian.net/img/common/qrcode_bgc.png",
  "navigat_img": "//img.xingkonglian.net/img/weapp/navigat_img.png",
  "nbImg1": "https://img.xingkonglian.net/img/common/pnged1.png",
  "nbImg2": "//img.xingkonglian.net/img/weapp/nbImg2.png",
  "nbImg3": "//img.xingkonglian.net/img/weapp/nbImg3.png",
  "nbImg4": "//img.xingkonglian.net/img/weapp/nbImg4.png",
  "nbImg5": "//img.xingkonglian.net/img/weapp/nbImg5.png",
  "nbImg6": "//img.xingkonglian.net/img/weapp/nbImg6.png",
  "none": "https://img.xingkonglian.net/img/common/no_order.png",
  "order_status0": "//img.xingkonglian.net/img/weapp/order_status0.png",
  "other": "//img.xingkonglian.net/img/weapp/other.png",
  "posterImg": "//img.xingkonglian.net/img/weapp/posterImg.png",
  "posterImg1": "//img.xingkonglian.net/img/weapp/posterImg1.png",
  "share": "//img.xingkonglian.net/img/weapp/share.png",
  "success_icon": "//img.xingkonglian.net/img/weapp/success_icon.png",
  "swiper": "//img.xingkonglian.net/img/weapp/swiper.png",
  "title1": "//img.xingkonglian.net/img/weapp/title1.png",
  "top_img": "https://img.xingkonglian.net/img/common/top_img1.png",
  "vip_title1": "//img.xingkonglian.net/img/weapp/vip_title1.png",
  "vip_title2": "//img.xingkonglian.net/img/weapp/vip_title2.png",
  "yingye": "//img.xingkonglian.net/img/weapp/yingye.png",
  "zhengmian": "//img.xingkonglian.net/img/weapp/zhengmian.png",
  "bargin_poster":"https://img.xingkonglian.net/img/common/bargin/bargin_poster.png"
}

const setTabBar = function (index) {
  const app = getApp();
  let length = [];
  app.globalData.tabList.forEach((item, index) => {
    if (!length[index]) length[index] = 0;
    item.forEach((_item, _index) => {
      if (_item.visible) length[index]++;
    })
  })
  this.getTabBar().setData({
    selected: index,
    tabList: app.globalData.tabList,
    length: length
  })
}

const setTabList = function (index, isShow) {
  const app = getApp();
  let length = [];
  app.globalData.tabList.forEach((item, i) => {
    if (!length[i]) length[i] = 0;
    item.forEach((_item, _i) => {
      if (_item.index == index) { console.log(_item); _item.visible = isShow; }
      if (_item.visible) { length[i]++; }
    })
  })
  console.log(length)
  console.log(app.globalData.tabList)
  this.getTabBar().setData({
    tabList: app.globalData.tabList,
    length: length
  })
}
const hideTabList = function (isShow) {
  const app = getApp();
  // let length = [];
  app.globalData.tabList.forEach((item, i) => {
    // if (!length[i]) length[i] = 0;
    item.forEach((_item, _i) => {
       _item.visible = isShow
    })
  })
  // console.log(length)
  console.log(app.globalData.tabList)
  this.getTabBar().setData({
    tabList: app.globalData.tabList,
    // length: 4
  })
}

const jsonParamToQueryStr =function(options){ // {a:'1',b:2} => a=1&b=2

// encodeURIComponent
 /* var params = Object.keys(options).map(function (key) { 
     
    return key + "=" + options[key]; 
  }).join("&");*/

   let keys=Object.keys(options)
   console.log('===keys',keys)
    let param =''
   
    for (var i=0;i<keys.length;i++){
       if(i!=0){
          param = param +"&"
       }
       param= param +keys[i]+'='+options[keys[i]]
       
    }

    return param
}

const videoGifGet= function (is_test, value,p,item_id) { // video gif get

  var BaseUrl_8098 = is_test ? 'https://api.comm.t.xingkonglian.net:8098' : 'https://api.comm.xingkonglian.net:8098';

  var biz_id=p+'_'+item_id
  var link= BaseUrl_8098 + "/api/v1/common/video_gif_store?url=" + value + '&offset=4&t=4&r=4&s=256*256&biz_id='+biz_id
  console.log('------gif link:'+link)
  return link
}

// 该功能开发完成
const openRebatOrderAll=function(p){ //打开返利订单 第三方小程序,全部订单

   if("PDD"==p){

      wx.navigateToMiniProgram({
        appId: 'wx32540bd863b27570',
        path: 'pages/personal/personal',
        success(){
        
        },
        fail(res) {
          
        }
      })
   }else if("JD"==p){
     
      wx.navigateToMiniProgram({
        appId: 'wx91d27dbf599dff74',
        path: 'pages/order/list/list',
        success(){
        
        },
        fail(res) {
          
        }
      })
   }else if("VIP"==p){
     
      wx.navigateToMiniProgram({
        appId: 'wxe9714e742209d35f',
        path: 'vipshop/order/pages/orderList/orderList?status=all',
        success(){
        
        },
        fail(res) {
          
        }
      })
   }else if("TB"==p){
     
      wx.showToast({
        title: '您好，该功能升级中...',
      })
   }

}


// 该功能开发完成
const openRebatOrderDetail=function(p,order_no,item_id){ //打开返利订单 第三方小程序,查看 订单详情页面

  if("PDD"==p){

     wx.navigateToMiniProgram({
       appId: 'wx32540bd863b27570',
       path: 'package_e/order/order?ordersn='+order_no+'&type=0',
       success(){
       
       },
       fail(res) {
         
       }
     })
  }else if("JD"==p){
    
      // 目前京东的订单号 是内部传递，不是通过参数传递，所有获取不到，那就直接显示全部订单页
     wx.navigateToMiniProgram({
      appId: 'wx91d27dbf599dff74',
      path: 'pages/order/list/list',  ////pages/order/detail/detail.html?__pid=Pxo2oq9ameg
      success(){
      
      },
      fail(res) {
        
      }
    })
  }else if("VIP"==p){
    
     wx.navigateToMiniProgram({
       appId: 'wxe9714e742209d35f',
       path: 'vipshop/order/pages/orderDetail/index?order_sn='+order_no+'&order_type=0',
       success(){
       
       },
       fail(res) {
         
       }
     })
  }else if("TB"==p){
    
     wx.showToast({
       title: '您好，该功能升级中...',
     })
  }else if('XP'==p){

    
    wx.navigateTo({
      url: '/pages/module/orderDetail/orderDetail?id='+order_no,
    })

  }

}


const kuaidi_search_byorderno=function(p,order_no,item_id,mall_id){ // 第三方各 电商平台 根据订单号 查看物流，直接跳转到物流页面

  if("PDD"==p){

    wx.navigateToMiniProgram({
      appId: 'wx32540bd863b27570',
      path: 'pages/web/web?page_name=goods_express&order_sn='+order_no+'&mall_id='+mall_id+'&goods_id='+item_id,
      success(){
      
      },
      fail(res) {
        
      }
    })
 }else if("JD"==p){
   
    wx.navigateToMiniProgram({
      appId: 'wx91d27dbf599dff74',
      path: 'pages/order/list/list',  // pages/order/follow/follow.html?__pid=Px8ff6p0v1hh6 没开放 还是走订单
      success(){
      
      },
      fail(res) {
        
      }
    })
 }else if("VIP"==p){
   // vipshop/order/pages/logisticsInfo/logisticsInfo.html?orderSn=19093022796546
    wx.navigateToMiniProgram({
      appId: 'wxe9714e742209d35f',
      path: 'vipshop/order/pages/logisticsInfo/logisticsInfo?orderSn='+order_no,
      success(){
      
      },
      fail(res) {
        
      }
    })
 }else if("TB"==p){
   
    wx.showToast({
      title: '您好，该功能升级中...',
    })
 }else if('XP'==p){

    // 暂不实现 使用订单详情即可

 }
}

const kuaidi_search=function(nu){ //第三方小程序快递查下 快递100;直接根据快递单号查;可以用于星品订单
  wx.navigateToMiniProgram({
    appId: 'wx6885acbedba59c14',
    path: 'pages/result/result?nu='+nu+'&querysource=indexpage',
    success(){
    
    },
    fail(res) {
      
    }
  })
}


const go_history_page = function(page_path,is_reload){ // 跳转到指定的 历史页面栈， 通过 path指定,不需要 斜杠   is_reload: true|false 执行页面 reload方法/
  var pages = getCurrentPages();

  let size = pages.length;

  for(var i=size-1;i>=0;i--){

     let page=pages[i] //
     
     console.log('---read cur_page:',page.route)
     if(page.route == page_path){
   
        if (is_reload && page.reload){
          page.reload()
        }

        wx.navigateBack({ 
          delta: size-i-1,  //上一个PAGE
        })
        return 
     }
  }

}

const tip_func = function(msg){

  wx.showModal({
    title: '小提示',
    content:msg,
    })  
}


const regex_phone =function(v){

  var phone_regex=new RegExp(/^1{1}[0-9]{10}$/,'g');
  let is_phone=phone_regex.test(v)

  return is_phone
}

const  preBindRelation=function(options,globalData){ //预绑定关系,尽量在onLoad中完成调用

  if(options){
    // 设置关系
   if(options.share_uid && !globalData.share_uid){
        globalData.share_uid = options.share_uid
   }

   if(options.share_openid && !globalData.share_openid){
      globalData.share_openid = options.share_openid
   }

  }
  console.log('----share_uid:',globalData.share_uid)
  console.log('------share_openid:',globalData.share_openid)
}





module.exports = {
  formatTime: formatTime,
  img_list: img_list,
  setTabBar: setTabBar,
  hideTabList:hideTabList,
  setTabList: setTabList,
  jsonParamToQueryStr:jsonParamToQueryStr,
  videoGifGet:videoGifGet,
  openRebatOrderDetail:openRebatOrderDetail,
  kuaidi_search:kuaidi_search,
  kuaidi_search_byorderno:kuaidi_search_byorderno,
  go_history_page:go_history_page,
  tip_func:tip_func,
  regex_phone:regex_phone,
  preBindRelation:preBindRelation,
  formatDate:formatDate,
  is_video:is_video
}